package net.b521.petshop.service.impl;

import net.b521.petshop.dao.ClassificationMapper;
import net.b521.petshop.dao.model.Classification;
import net.b521.petshop.dao.model.ClassificationExample;
import net.b521.petshop.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ClassificationServiceImpl implements ClassificationService {


    @Autowired
    private ClassificationMapper classificationMapper;

    @Override
    public Classification findById(int id) {
        // return classificationDao.getOne(id);
        return classificationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Classification> findAll(int type) {
        ClassificationExample classificationExample = new ClassificationExample();
        classificationExample.createCriteria().andTypeEqualTo(type);
        List<Classification> classifications = classificationMapper.selectByExample(classificationExample);
        // return classificationDao.findByType(type);
        return classifications;
    }


    @Override
    public void update(Classification classification) {
        int i = classificationMapper.updateByPrimaryKeySelective(classification);
        System.out.println(i);
        // classificationDao.save(classification);
    }

    @Override
    public int create(Classification classification) {
        int i = classificationMapper.insertSelective(classification);
        // Classification classification1 = classificationDao.save(classification);
        // return classification1.getId();
        return i;
    }

    @Override
    public void delById(int id) {
        // classificationDao.delete(id);
        classificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 通过一级分类id查找它所有的二级分类
     *
     * @param cid
     * @return
     */
    @Override
    public List<Classification> findByParentId(int cid) {
        ClassificationExample classificationExample = new ClassificationExample();
        classificationExample.createCriteria().andParentIdEqualTo(cid);
        return classificationMapper.selectByExample(classificationExample);
        // return classificationDao.findByParentId(cid);
    }

}
